package com.drawingtimer.admin.controller;

import com.drawingtimer.admin.model.entity.TProcessFlowCard;
import com.drawingtimer.admin.model.qo.TProcessFlowCardQO;
import com.drawingtimer.admin.model.vo.TProcessFlowCardPageVO;
import com.drawingtimer.admin.model.vo.TProcessInfoPageVO;
import com.drawingtimer.admin.service.TProcessFlowCardService;
import com.hccake.ballcat.common.log.operation.annotation.CreateOperationLogging;
import com.hccake.ballcat.common.log.operation.annotation.DeleteOperationLogging;
import com.hccake.ballcat.common.log.operation.annotation.UpdateOperationLogging;
import com.hccake.ballcat.common.model.domain.PageParam;
import com.hccake.ballcat.common.model.domain.PageResult;
import com.hccake.ballcat.common.model.result.BaseResultCode;
import com.hccake.ballcat.common.model.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 工序流转卡信息
 *
 * @author jjh 2025-04-24 16:34:16
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/ProductionProcesses/t-process-flow-card")
@Tag(name = "工序流转卡信息管理")
public class TProcessFlowCardController {

	private final TProcessFlowCardService tProcessFlowCardService;

	/**
	 * 分页查询
	 * @param pageParam 分页参数
	 * @param tProcessFlowCardQO 工序流转卡信息查询对象
	 * @return R 通用返回体
	 */
	@Operation(summary = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@per.hasPermission('ProductionProcesses:t-process-flow-card:read')")
	public R<PageResult<TProcessFlowCardPageVO>> getTProcessFlowCardPage(PageParam pageParam,
			TProcessFlowCardQO tProcessFlowCardQO) {
		return R.ok(tProcessFlowCardService.queryPage(pageParam, tProcessFlowCardQO));
	}

	/**
	 * 新增工序流转卡信息
	 * @param tProcessFlowCard 工序流转卡信息
	 * @return R 通用返回体
	 */
	@Operation(summary = "新增工序流转卡信息")
	@CreateOperationLogging(msg = "新增工序流转卡信息")
	@PostMapping
	@PreAuthorize("@per.hasPermission('ProductionProcesses:t-process-flow-card:add')")
	public R<Void> save(@RequestBody TProcessFlowCard tProcessFlowCard) {
		return tProcessFlowCardService.save(tProcessFlowCard) ? R.ok()
				: R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "新增工序流转卡信息失败");
	}

	/**
	 * 修改工序流转卡信息
	 * @param tProcessFlowCard 工序流转卡信息
	 * @return R 通用返回体
	 */
	@Operation(summary = "修改工序流转卡信息")
	@UpdateOperationLogging(msg = "修改工序流转卡信息")
	@PutMapping
	@PreAuthorize("@per.hasPermission('ProductionProcesses:t-process-flow-card:edit')")
	public R<Void> updateById(@RequestBody TProcessFlowCard tProcessFlowCard) {
		return tProcessFlowCardService.updateById(tProcessFlowCard) ? R.ok()
				: R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "修改工序流转卡信息失败");
	}

	/**
	 * 通过id删除工序流转卡信息
	 * @param id id
	 * @return R 通用返回体
	 */
	@Operation(summary = "通过id删除工序流转卡信息")
	@DeleteOperationLogging(msg = "通过id删除工序流转卡信息")
	@DeleteMapping("/{id}")
	@PreAuthorize("@per.hasPermission('ProductionProcesses:t-process-flow-card:del')")
	public R<Void> removeById(@PathVariable("id") Long id) {
		return tProcessFlowCardService.removeById(id) ? R.ok()
				: R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "通过id删除工序流转卡信息失败");
	}

	/**
	 * 通过流转卡号查看工序流转卡关联工序详细信息
	 * @param flowCardNumber flowCardNumber
	 * @return R 通用返回体
	 */
	@Operation(summary = "通过id查看工序流转卡关联工序详细信息")
	@GetMapping("/{flowCardNumber}")
	@PreAuthorize("@per.hasPermission('ProductionProcesses:t-process-flow-card:details')")
	public R<PageResult<TProcessInfoPageVO>> getTProcessFlowCardById(
			@PathVariable("flowCardNumber") String flowCardNumber) {
		return R.ok(tProcessFlowCardService.getDetailsById(flowCardNumber));
	}

	/**
	 * 导出
	 */
	@GetMapping("/export/{id}")
	@PreAuthorize("@per.hasPermission('ProductionProcesses:t-process-flow-card:export')")
	public void export(@PathVariable("id") Long id, HttpServletResponse response) {
		tProcessFlowCardService.export(id, response);
	}

	/**
	 * 报工
	 * @param flowCardNumber flowCardNumber
	 * @return R 通用返回体
	 */
	@Operation(summary = "通过id查看工序流转卡关联工序详细信息")
	@GetMapping("reportingForWork")
	public R<PageResult<TProcessInfoPageVO>> reportingForWork(@RequestParam("flowCardNumber") String flowCardNumber) {
		return R.ok(tProcessFlowCardService.getReportingForWorkByFlowCardNumber(flowCardNumber));
	}

	/**
	 * 从ERP 获取数据工序数据
	 */
	@GetMapping("/getDataFromErp")
	public R<Void> getDataFromErp() {
		if (tProcessFlowCardService.getDataFromErp()) {
			return R.ok();
		}
		else {
			return R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "ERP数据获取失败");
		}
	}

	/**
	 * 从ERP 获取数据工序数据 指定MO编号
	 */
	@GetMapping("/getDataFromErpByMo")
	public R<Void> getDataFromErpByMo(String flowCardNumber) {
		return tProcessFlowCardService.getDataFromErpByMo(flowCardNumber) ? R.ok()
				: R.failed(BaseResultCode.UPDATE_DATABASE_ERROR, "ERP数据获取失败");
	}

}